package p.sglmsn.top.apale;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;

import java.io.File;
import java.io.InputStream;

public class ImageTabController {
    @FXML
    private ImageView imageView;

    @FXML
    private TextField imagePathField;

    @FXML
    private Button imageBrowseButton;
    
    @FXML
    private VBox imageBox;

    private File selectedImageFile;

    long imageSize;

    @FXML
    private void initialize() {
        // 添加拖拽支持
        setupDragAndDrop();
        
        // 加载默认图片
        loadDefaultImage();
    }

    @FXML
    private void browseImage(ActionEvent event) {
        FileChooser fileChooser = new FileChooser();
        fileChooser.setTitle("选择图片文件");
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("图片文件", "*.png", "*.jpg", "*.jpeg", "*.gif", "*.bmp"),
                new FileChooser.ExtensionFilter("所有文件", "*.*")
        );

        File file = fileChooser.showOpenDialog(imageBrowseButton.getScene().getWindow());
        if (file != null) {
            selectImage(file);
        }
    }

    private void setupDragAndDrop() {
        imageBox.setOnDragOver(event -> {
            if (event.getGestureSource() != imageBox && event.getDragboard().hasFiles()) {
                event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
            }
            event.consume();
        });

        imageBox.setOnDragDropped(event -> {
            Dragboard dragboard = event.getDragboard();
            boolean success = false;
            if (dragboard.hasFiles()) {
                success = true;
                // 支持多文件或目录拖拽，但只使用第一个有效的图片文件
                for (File file : dragboard.getFiles()) {
                    selectImage(file);
                    break;
                }
            }
            event.setDropCompleted(success);
            event.consume();
        });
    }

    private void selectImage(File file) {
        // 检查文件是否为图片
        String fileName = file.getName().toLowerCase();
        if (fileName.endsWith(".png") || fileName.endsWith(".jpg") || fileName.endsWith(".jpeg") || 
            fileName.endsWith(".gif") || fileName.endsWith(".bmp")) {
            selectedImageFile = file;
            imageSize = file.length();
            imagePathField.setText(file.getAbsolutePath());
            Image image = new Image(file.toURI().toString());
            imageView.setImage(image);
        } else {
            // 如果不是图片文件，显示错误信息
            imagePathField.setText("请选择有效的图片文件！");
        }
    }
    
    private void loadDefaultImage() {
        try {
            // 尝试加载默认图片
            InputStream imageStream = getDefault();

            if (imageStream != null) {
                imageSize=imageStream.available();
                Image image = new Image(imageStream);
                imageView.setImage(image);
                imagePathField.setText("使用默认图片");
            }
        } catch (Exception e) {
            System.out.println("无法加载默认图片: " + e.getMessage());
        }
    }

    private InputStream getDefault() {
        return getClass().getResourceAsStream("/a.jpg");
    }

    public File getSelectedImageFile() {
        return selectedImageFile;
    }

    public long getImgSize() {
        return imageSize;
    }
    
    public boolean isDefaultImage() {
        return selectedImageFile == null;
    }
}